package TeacherEvaluation.com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Department;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.entity.Student;
import TeacherEvaluation.com.entity.Teacher;
import TeacherEvaluation.com.service.departmentService;
import TeacherEvaluation.com.service.teacherService;

/**
 * 教师信息控制类
 * @author 周江燕
 *
 */

@Controller
public class teacherController {
	
	@Autowired
	teacherService teacherService;
	@Autowired
	departmentService departmentService;
	
	//主页
	@RequestMapping("/showTeacher")
	@ResponseBody
	public ModelAndView showTeacher() {		
		List<Teacher> teachers = teacherService.selectTeacher();
		ModelAndView mView = new ModelAndView("admin/teacher/showTeacher");
		mView.addObject("teachers",teachers);
		return mView;
	}
		
	//添加教师
	@RequestMapping("/addTeacher")
	@ResponseBody
	public ModelAndView addTeacher() {
		ModelAndView mView = new ModelAndView("admin/teacher/addTeacher");
		mView.addObject("departments",departmentService.selectDep());
		return mView;
	}
	
	
	@RequestMapping("/addTeacher_submit")
	@ResponseBody
	public ResultMsg addTeacher(Teacher teacher) {
		int i = teacherService.insertTeacher(teacher);
		if(i>0) {
			return new ResultMsg(1, "添加老师成功！");
		}else if(i==-2){
			return new ResultMsg(2, "该老师已经存在！");
		}
		return new ResultMsg(0, "添加老师失败");
	}
		
	//修改老师
	@RequestMapping("/updateTeaIndex")
	@ResponseBody
	public ModelAndView updateTeacher(String work_number) {
		ModelAndView mView = new ModelAndView("admin/teacher/updateTeacher");
		mView.addObject("Teacher",teacherService.selectTeacherByOne(work_number));
		mView.addObject("departments",departmentService.selectDep());
		return mView;
	}
	
	@RequestMapping("/updataTeacher")
	@ResponseBody
	public ResultMsg updataTeacher(Teacher teacher) {
		int i = teacherService.updataTeacher(teacher);
		if(i==0) {
			return new ResultMsg(0, "修改老师失败！");
		}else {
			return new ResultMsg(1, "修改老师成功！");
		}
	}
	
	//删除老师
	@RequestMapping("/delTeacher")
	@ResponseBody
	public ResultMsg delTeacher(String work_number) {
		int i = teacherService.delTeacher(work_number);
		if(i==0) {
			return new ResultMsg(0, "删除老师失败！");
		}else {
			return new ResultMsg(1, "删除老师成功！");
		}
	}
	
	
	
	//教师查看个人信息
	@RequestMapping("/myMessage")
	public ModelAndView myMessage(HttpServletRequest request) {
		ModelAndView mView = new ModelAndView("teachers/myMessage");
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher) session.getAttribute("user");
		mView.addObject("user",teacher);
		//mView.addObject("Teacher",teacherService.selectTeacherByOne(work_number));
		mView.addObject("departments",departmentService.selectDep());
		return mView;
	}
	
	//修改密码
	@RequestMapping("/changePasswordIndex")
	@ResponseBody
	public ModelAndView changePassword(String te_password) {
		ModelAndView mView = new ModelAndView("teachers/changePasswordIndex");
		return mView;
	}
	
	@RequestMapping("/changePassword")
	@ResponseBody
	public ResultMsg changePassword(String te_name,HttpServletRequest request) {
		
		HttpSession session=request.getSession();
		Teacher teacher = (Teacher) session.getAttribute("user");
		teacher.setTe_password(te_name);
		
		int i = teacherService.changePassword(teacher);
		if(i==0) {
			return new ResultMsg(0, "修改密码失败！");
		}else {
			return new ResultMsg(1, "修改密码成功！");
		}
	}
	
	//查询未评教的老师
	@RequestMapping("/seleTS")
	public ModelAndView seleTS(HttpServletRequest request) {
		//获取student
		HttpSession session=request.getSession();
		
		Teacher user=(Teacher)session.getAttribute("user");
		ModelAndView mView = new ModelAndView("teachers/assessTeacherTable");
		mView.addObject("Teacher", teacherService.seleTS(user.getTe_id()));
		return mView;
	}
}
